package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "远程调用-购物车")//weball => cartFeign => service-cart
@RequestMapping("/api/inner/rpc/cart")
@RestController
public class CartRpcController {

    @Autowired
    CartService cartService;

    @ApiOperation("添加购物车")
    @GetMapping("/add/{skuId}/{skuNum}")
    public Result addToCart (@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum){

        log.info("添加到购物车");
        //1 选用哪个购物车  cart:info:用户标识
        String cartKey = cartService.determinCartKey();
        //2 添加到购物车      K:[cart:info:用户标识]    V:[hash(skuId 商品信息)]
        SkuInfo skuInfo = cartService.addToCart(skuId,skuNum,cartKey);
        return Result.ok(skuInfo);
    }

    @ApiOperation("删除选中的商品")
    @GetMapping("/deleteChecked")
    public Result deleteChecked(){
        //
        String cartKey = cartService.determinCartKey();
        cartService.deleteChecked(cartKey);
        return Result.ok();
    }

    @ApiOperation("获取所有选中的商品")
    @GetMapping("/checkeds")
    public Result<List<CartInfo>> getCheckeds(){
        String cartKey = cartService.determinCartKey();
        List<CartInfo> list= cartService.getCheckeds(cartKey);
        return Result.ok(list);
    }
}
